package com.yzt.base.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yzt.base.entity.BaseUser;
import com.yzt.sys.entity.User;
import com.yzt.sys.service.IUserService;

@Component
public class UserDetailService implements UserDetailsService {

	@Autowired
	private IUserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) {
		User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
		if (user == null) {
			throw new UsernameNotFoundException("用户名不存在");
		}

		if (user.getStatus().equals("1")) {
			throw new DisabledException("账户已禁用");
		}

		List<GrantedAuthority> list = new ArrayList<>();

		BaseUser baseUser = new BaseUser(username, user.getPassword(), list);
		baseUser.setId(user.getId());
		baseUser.setNickname(user.getNikename());
		return baseUser;
	}

}
